<template>
	<view class="login-page">
		<image class="bg" :src="login_bg" mode="aspectFill"></image>

		


		<view class="content">
			<image class="logo" :src="logo" mode="aspectFit"></image>
			<text class="brand">{{companyInfo.company_pinpai}}</text>
			<!-- #ifdef H5 -->
			<button class="btn-primary" @tap="onLoginWx">一键登录</button>
			<!-- #endif -->
			<!-- #ifdef MP-WEIXIN -->
			<button open-type="getPhoneNumber" class="btn-primary" :disabled="logining" @click="" @getphonenumber="decryptPhoneNumber">一键登录</button>
			<!-- #endif -->
			<!-- <view class="phone-login" @tap="goPhoneLogin">鲜链集配手机号登录/注册 ></view> -->

			<label class="agree">
				<checkbox :checked="agreed" color="#244a9a" @tap.stop="toggleAgree" />
				<text>我已阅读并同意</text>
				<text class="link" @tap.stop="openProtocol()">《用户说明及隐私协议》</text>
			</label>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'LoginPage',
		data() {
			return {
				agreed: false,
				logining: false,
				checkbox: false,
				login_bg: this.$constPath + 'Images/login_bg.jpg',
				logo: '',
				statusBarHeight: 10,
				companyInfo: {}
			}
		},
		onLoad(option) {
			// #ifdef MP-WEIXIN
			const systemInfo = uni.getSystemInfoSync();
			this.statusBarHeight = systemInfo.statusBarHeight; // 获取状态栏高度
			// #endif

			this.source = option.source;

			uni.login({
				success: (res) => {
					this.code = res.code;
				}
			});

			this.getCompanyInfo();
		},
		methods: {
			
			navBack(){
				uni.switchTab({
				    url: '/pages/index/index'
				});
			},
			
			toggleAgree() {
				this.agreed = !this.agreed
			},
			getCompanyInfo() {
				uni.request({
					method: 'POST',
					url: this.$constPath + 'Api/Admin/System.ashx',
					data: JSON.stringify({
						action: 'CompanyOneByAppID',
						appid: this.$constAppId
					}),
					header: { 'content-type': "application/x-www-form-urlencoded" },
					success: (res) => {
						if (res.data.code == 0) {
							this.companyInfo = res.data.data;
							this.logo = this.$picServerPath + "Upload/logo/" + this.companyInfo.company_logo;
						}
					}
				});
			},
			// #ifdef H5
			onLoginWx() {
				if (!this.agreed) {
					uni.showToast({ title: '请先同意协议', icon: 'none' })
					return
				}
				this.logining = true;
				window.location.href = this.$constPath + 'WXLogin.aspx?appid=' + this.$constAppId + '&member_code=' + this.mobile + '&member_pass=' + this.password + '&member_member=' + (uni.getStorageSync('member_member_' + this.$constAppId) || 0) + '&member_store=' + (uni.getStorageSync('member_store_' + this.$constAppId) || 0);
				// uni.showModal({
				// 	title: '重要提示！！！',
				// 	content: '如果你已有帐号，且从未在公众号中登陆过，请填写手机号和密码，我们将为您进行绑定。否则本次操作将会注册一个新的账号。确定要微信授权登陆吗！！！',
				// 	showCancel: true,
				// 	success: (res) => {
				// 		if (res.confirm) {
				// 		}
				// 	}
				// });
			},
			// #endif
			// #ifdef MP-WEIXIN
			decryptPhoneNumber(e) {
				if (!this.agreed) {
					uni.showToast({ title: '请先同意协议', icon: 'none' })
					return
				}
				if (e.detail.encryptedData) {
					let encryptedData = e.detail.encryptedData;
					let iv = e.detail.iv;
					if (this.logining == true) return;
					this.logining = true;
					uni.showLoading({
						title: '登录中..',
						mask: true
					});
					uni.request({
						method: 'POST',
						url: this.$constPath + 'Api/Admin/Member.ashx',
						data: JSON.stringify({
							action: 'GetPhoneNumber',
							appid: this.$constAppId,
							code: this.code,
							encryptedData: encryptedData,
							iv: iv,
							member_member: uni.getStorageSync('member_member_' + this.$constAppId) || 0,
							member_store: uni.getStorageSync('store_' + this.$constAppId) || 0
						}),
						header: { 'content-type': "application/x-www-form-urlencoded" },
						success: (res) => {
							uni.hideLoading();
							this.logining = false;

							if (res.data.code == 0) {
								uni.setStorageSync('memberInfo_' + this.$constAppId, res.data.data);
								if (this.source > 0) {
									this.$api.prePage().memberInfo = res.data.data;
									this.$api.prePage().logincomplete();
								}
								if (res.data.attr != '') {
									uni.showModal({
										content: res.data.attr,
										showCancel: false,
										complete: (e) => {
											uni.navigateBack();
										}
									});
								} else {
									if (this.source > 0) {
										uni.navigateBack();
									} else {
										uni.switchTab({
											url: '/pages/index/index'
										});
									}
								}
							} else {
								this.$api.msg(res.data.msg);
								this.logining = false;
							}
						}
					});
				} else {

				}
			},
			// #endif
			goPhoneLogin() {
				// uni.navigateTo({
				// 	url: `/pages/public/loginPhone`
				// });
				this.toNav(`public/loginPhone`)
			},
			openProtocol() {
				uni.navigateTo({
					url: `webview?name=用户协议与隐私政策&path=${this.$constPath}yszc.html`
				});
				// this.toNav(`webview?name=用户协议与隐私政策&path=${this.$constPath}yszc.html`)
				
				
			}
		}
	}
</script>

<style lang="less" scoped>
	.login-page {
		position: relative;
		width: 100vw;
		min-height: 100vh;
		overflow: hidden;
		background: #eaf4ff;

		.status-bar {
			height: var(--status-bar-height);
			width: 100%;
		}

		.bg {
			position: absolute;
			inset: 0;
			width: 100%;
			height: 100%;
			z-index: 0;
		}

		.login-header {
			z-index: 12;
			position: relative;
			padding-top: var(--status-bar-height);
			
			.login-header-box{
				padding: 40rpx 10rpx 20rpx 10rpx;
				display: flex;
				
				.back-btn{
					font-size: 40upx;
					color: #ffffff;
					position: absolute;
				}
				
				.login-header-title{
					font-size: 34rpx;
					text-align: center;
					color: #fff;
					width: 100%;

				}
			}
		}

		.content {
			position: relative;
			z-index: 1;
			padding: 260rpx 0 0 0;
			display: flex;
			flex-direction: column;
			align-items: center;
			text-align: center;
		}

		.logo {
			width: 200rpx;
			height: 200rpx;
			border-radius: 50%;
			margin: 40rpx 0 24rpx;
		}

		.brand {
			font-size: 48rpx;
			color: #244a9a;
			font-weight: 700;
			letter-spacing: 4rpx;
			margin-bottom: 48rpx;
		}

		.btn-primary {
			width: 80%;
			height: 88rpx;
			line-height: 88rpx;
			background: #244a9a;
			color: #fff;
			border-radius: 12rpx;
			margin: 28rpx auto;
		}

		.phone-login {
			color: #244a9a;
			font-weight: 600;
			margin-bottom: 32rpx;
		}

		.agree {
			display: flex;
			align-items: center;
			font-size: 26rpx;
			color: #333;

			.link {
				color: #244a9a;
				margin-left: 8rpx;
			}
		}
	}
</style>